/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Interfaz.Altas;

import static Auxiliares.MensajesCodigosError.CAMPO_OBLIGATORIO;
import Controladores.ControladorCamposTexto;
import Controladores.GestorEmpleados;
import Controladores.GestorMensajes;
import Modelo.Empleado;
import java.awt.Color;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrador
 */
public class InterfazAltaEmpleado extends javax.swing.JDialog {

    /**
     * Creates new form InterfazAltaEmpleado
     * @param _pantallaPadre
     * @param _modal
     */
    public InterfazAltaEmpleado(
     java.awt.Frame _pantallaPadre,
     boolean _modal
    ) {
        super(_pantallaPadre,"Alta Empleado", _modal);
        initComponents();
        setLocationRelativeTo(_pantallaPadre);

        establecerFondo();
        controladorCampos = new ControladorCamposTexto();
        administradorMensajes = new GestorMensajes();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabelNombre = new javax.swing.JLabel();
        jTextFieldNombre = new javax.swing.JTextField();
        jLabelApellidos = new javax.swing.JLabel();
        jTextFieldApellidos = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jLabelClaveEmpleado = new javax.swing.JLabel();
        jTextFieldClaveEmpleado = new javax.swing.JTextField();
        jLabelTipoEmpleado = new javax.swing.JLabel();
        jComboBoxTipoEmpleado = new javax.swing.JComboBox();
        jPanel3 = new javax.swing.JPanel();
        jButtonCancelar = new javax.swing.JButton();
        jButtonAceptar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setOpaque(false);

        jLabelNombre.setText("*Nombre(s)");

        jTextFieldNombre.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldNombreFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldNombreFocusLost(evt);
            }
        });
        jTextFieldNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldNombreKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldNombreKeyTyped(evt);
            }
        });

        jLabelApellidos.setText("*Apellidos");

        jTextFieldApellidos.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldApellidosFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldApellidosFocusLost(evt);
            }
        });
        jTextFieldApellidos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldApellidosKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldApellidosKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelNombre)
                    .addComponent(jLabelApellidos))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldApellidos, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                    .addComponent(jTextFieldNombre))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNombre)
                    .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelApellidos)
                    .addComponent(jTextFieldApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setOpaque(false);

        jLabelClaveEmpleado.setText("*Clave empleado");

        jTextFieldClaveEmpleado.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldClaveEmpleadoFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldClaveEmpleadoFocusLost(evt);
            }
        });
        jTextFieldClaveEmpleado.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldClaveEmpleadoKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldClaveEmpleadoKeyTyped(evt);
            }
        });

        jLabelTipoEmpleado.setText("*Tipo Empleado");

        jComboBoxTipoEmpleado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione uno", "Maestro", "Administrativo" }));
        jComboBoxTipoEmpleado.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jComboBoxTipoEmpleadoKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBoxTipoEmpleado, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabelClaveEmpleado)
                                .addGap(18, 18, 18)
                                .addComponent(jTextFieldClaveEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabelTipoEmpleado))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelClaveEmpleado)
                    .addComponent(jTextFieldClaveEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabelTipoEmpleado)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBoxTipoEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setOpaque(false);

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });
        jButtonCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButtonCancelarKeyPressed(evt);
            }
        });

        jButtonAceptar.setText("Aceptar");
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });
        jButtonAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButtonAceptarKeyPressed(evt);
            }
        });

        jLabel2.setText("Los campos marcados con asterisco(*)");

        jLabel3.setText("son obligatorios");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonAceptar)
                .addGap(18, 18, 18)
                .addComponent(jButtonCancelar)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonCancelar)
                        .addComponent(jButtonAceptar))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldNombreKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldNombreKeyPressed
        controladorCampos.enterTab(evt);
    }//GEN-LAST:event_jTextFieldNombreKeyPressed

    private void jTextFieldNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldNombreKeyTyped
        if(controladorCampos.formatoAlfabetico(evt)){
            controladorCampos.formatoMayusculas(evt);
        }
    }//GEN-LAST:event_jTextFieldNombreKeyTyped

    private void jTextFieldNombreFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldNombreFocusLost

        if(controladorCampos.esCampoTextoEnBlanco(jTextFieldNombre)){
            administradorMensajes.mensajeError(CAMPO_OBLIGATORIO, jTextFieldNombre);
        }
    }//GEN-LAST:event_jTextFieldNombreFocusLost

    private void jTextFieldNombreFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldNombreFocusGained
        controladorCampos.campoEnUso(jTextFieldNombre);
    }//GEN-LAST:event_jTextFieldNombreFocusGained

    private void jTextFieldApellidosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldApellidosKeyPressed
        controladorCampos.enterTab(evt);
    }//GEN-LAST:event_jTextFieldApellidosKeyPressed

    private void jTextFieldApellidosKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldApellidosKeyTyped
        if(controladorCampos.formatoAlfabetico(evt)){
            controladorCampos.formatoMayusculas(evt);
        }
    }//GEN-LAST:event_jTextFieldApellidosKeyTyped

    private void jTextFieldApellidosFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldApellidosFocusLost

        if(controladorCampos.esCampoTextoEnBlanco(jTextFieldApellidos)){
            administradorMensajes.mensajeError(CAMPO_OBLIGATORIO, jTextFieldApellidos);
        }
    }//GEN-LAST:event_jTextFieldApellidosFocusLost

    private void jTextFieldApellidosFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldApellidosFocusGained
        controladorCampos.campoEnUso(jTextFieldApellidos);
    }//GEN-LAST:event_jTextFieldApellidosFocusGained

    private void jTextFieldClaveEmpleadoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldClaveEmpleadoKeyPressed
        controladorCampos.enterTab(evt);
    }//GEN-LAST:event_jTextFieldClaveEmpleadoKeyPressed

    private void jTextFieldClaveEmpleadoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldClaveEmpleadoKeyTyped
        controladorCampos.formatoNumerico(evt);
    }//GEN-LAST:event_jTextFieldClaveEmpleadoKeyTyped

    private void jTextFieldClaveEmpleadoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldClaveEmpleadoFocusLost
 
        if(controladorCampos.esCampoTextoEnBlanco(jTextFieldClaveEmpleado)){
            administradorMensajes.mensajeError(CAMPO_OBLIGATORIO, jTextFieldClaveEmpleado);
        }
    }//GEN-LAST:event_jTextFieldClaveEmpleadoFocusLost

    private void jTextFieldClaveEmpleadoFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldClaveEmpleadoFocusGained
        controladorCampos.campoEnUso(jTextFieldClaveEmpleado);
    }//GEN-LAST:event_jTextFieldClaveEmpleadoFocusGained

    private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAceptarActionPerformed
        registrarEmpleado();
    }//GEN-LAST:event_jButtonAceptarActionPerformed

    private void jButtonAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButtonAceptarKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            registrarEmpleado();
        }
    }//GEN-LAST:event_jButtonAceptarKeyPressed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        cerrar();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButtonCancelarKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            cerrar();
        }
    }//GEN-LAST:event_jButtonCancelarKeyPressed

    private void jComboBoxTipoEmpleadoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jComboBoxTipoEmpleadoKeyPressed
        controladorCampos.enterTab(evt);
    }//GEN-LAST:event_jComboBoxTipoEmpleadoKeyPressed
    
    /**
     * Construye al empleado en base a los datos de los campos
     * @return Devuelve el empleado
     */
    private Empleado construirEmpleado(){
        Empleado nuevoEmpleado = new Empleado();
        
        String nombre = jTextFieldNombre.getText();
        String apellidos = jTextFieldApellidos.getText();
        nombre = nombre + " "  +apellidos;
        nuevoEmpleado.establecerNombre(nombre);
        
        String claveEmpleado = jTextFieldClaveEmpleado.getText();
        nuevoEmpleado.establecerClaveEmpleado(claveEmpleado);
        
        String tipoEmpleado = jComboBoxTipoEmpleado.getSelectedItem().toString();
        nuevoEmpleado.establecerTipoEmpleado(tipoEmpleado);
        
        return nuevoEmpleado;
    }
    
    /**
     * comprueba que todos los campos esten libres de errores
     * @return devuelve verdadero si y solo si todos los campos no presentan errores
     */
    private boolean sonCamposCorrectos(){
        boolean datosCorrectos = true;
        
        if(jTextFieldNombre.getToolTipText() == null
                && jTextFieldApellidos.getToolTipText() == null
                && jTextFieldClaveEmpleado.getToolTipText() == null
                && esTipoEmpleadoCorrecto()){
        
            return datosCorrectos;
        }else{
            return !datosCorrectos;
        }
    }
    
    /**
     * @return Devuelve verdadero si y solo si se selecciono un tipo de empleado
     */
    private boolean esTipoEmpleadoCorrecto(){
        return jComboBoxTipoEmpleado.getSelectedIndex() != 0;
    }
    
    /**
     * solicita la confirmacion para dar de alta al empleado
     * @return verdadero en caso de ser aceptada la nueva alta
     */
    private boolean confirmarAlta(){
        boolean AltaConfirmada = true;
        int ConfirmarAlta = JOptionPane.showConfirmDialog(
                    null,
                    "¿Desea dar de alta al empleado?",
                    "Alta de Empleado",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.INFORMATION_MESSAGE
                    );
        
        if(ConfirmarAlta == JOptionPane.OK_OPTION){
            return AltaConfirmada;
        }else{
            return !AltaConfirmada;
        }// IF
    }
    
    /**
     * cierra la ventana de registro de empleados
     */
    private void cerrar(){
        dispose();
    }
    
    /**
     * establece el fondo de color blanco
     */
    private void establecerFondo(){
        //cambia el color dle jframe
        this.getContentPane().setBackground(Color.white);
    }
    
    /**
     * inicia el proceso de registro para el nuevo empleado
     */
    private void registrarEmpleado(){
        if(sonCamposCorrectos()){
            if(confirmarAlta()){   
                Empleado nuevoEmpleado = construirEmpleado();
                GestorEmpleados administradorEmpelados = new GestorEmpleados();
                administradorEmpelados.registrar(nuevoEmpleado);
                cerrar();
            }
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JComboBox jComboBoxTipoEmpleado;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelApellidos;
    private javax.swing.JLabel jLabelClaveEmpleado;
    private javax.swing.JLabel jLabelNombre;
    private javax.swing.JLabel jLabelTipoEmpleado;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextFieldApellidos;
    private javax.swing.JTextField jTextFieldClaveEmpleado;
    private javax.swing.JTextField jTextFieldNombre;
    // End of variables declaration//GEN-END:variables
    private final GestorMensajes administradorMensajes;
    private final ControladorCamposTexto controladorCampos;
}